package com.sgq.vue.utils.resource;

import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONObject;
import com.sgq.vue.config.WeatherProperties;
import com.sgq.vue.entity.resource.City;
import com.sgq.vue.entity.resource.Weather;
import org.jsoup.Jsoup;
import springfox.documentation.spring.web.json.Json;

import java.io.IOException;
import java.util.ArrayList;
import java.util.List;
import java.util.Optional;

/**
 * @author: 83778
 * @date: 2023-12-23 19:55
 * @desc:
 */
public class WeatherUtil {

   public static List<Weather> getWeatherInfoOnline(List<Long> cityIds){
       List<String> jsonString = getJsonString(cityIds);
       List<Weather> weathers = new ArrayList<>();
       for (String s : jsonString) {
           weathers.add(JSONObject.parseObject(s,Weather.class));
       }
       return weathers;
   }

   public static List<String> getJsonString(List<Long> cityIds){
       String appid = WeatherProperties.APP_ID;
       String appsecret = WeatherProperties.APP_SECRET;
       List<String> weathers = new ArrayList<>();

       //判断菜单列表是否为空，如果不为空则使用菜单列表，否则创建集合对象
       Optional.ofNullable(cityIds)
               .orElse(new ArrayList<Long>())
               .stream()
               //先找到1级菜单
               .filter(item ->  item != null && item.toString().length() == 9 )
               .forEach( cityId -> {
                   String url = "http://v1.yiketianqi.com/free/day?appid="+ appid + "&appsecret=" + appsecret +"&cityid=" + cityId;
                   try {
                       String text = Jsoup.connect(url)
                               .timeout(3000)
                               .header("Host", "v1.yiketianqi.com")
                               .header("Accept-Encoding", "gzip, deflate")
                               .header("Accept-Language", "zh-CN,zh;q=0.9")
                               .header("X-Requested-With", "XMLHttpRequest")
                               .header("Content-Type", "application/json; charset=UTF-8")
                               .userAgent("Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/119.0.0.0 Safari/537.36")
                               .ignoreContentType(true)
                               .get()
                               .body()
                               .text();
                       weathers.add(text);
                   } catch (IOException e) {
                       e.printStackTrace();
                   }
               });
       return weathers;
   }

       public  static String sendRequest(String url){
           try {
               String text = Jsoup.connect(url)
                       .timeout(3000)
                       .header("Accept-Encoding", "gzip, deflate")
                       .header("Accept-Language", "zh-CN,zh;q=0.9")
                       .header("X-Requested-With", "XMLHttpRequest")
                       .header("Content-Type", "application/json; charset=UTF-8")
                       .userAgent("Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/119.0.0.0 Safari/537.36")
                       .ignoreContentType(true)
                       .get()
                       .body()
                       .text();
               return text;
           } catch (IOException e) {
               e.printStackTrace();
               return null;
           }

       }

       public static String simpleRequest(String url){
           try {
               String text = Jsoup.connect(url)
                       .timeout(3000)
                       .header("Accept-Encoding", "gzip, deflate")
                       .header("Accept-Language", "zh-CN,zh;q=0.9")
                       .header("X-Requested-With", "XMLHttpRequest")
                       .header("Content-Type", "application/json; charset=UTF-8")
                       .userAgent("Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/119.0.0.0 Safari/537.36")
                       .ignoreContentType(true)
                       .get()
                       .body()
                       .text();
                return text;
           } catch (IOException e) {
               e.printStackTrace();
           }
           return null;
       }


}
